package cn.jiyun.controller;

import cn.jiyun.constant.MessageConstant;
import cn.jiyun.constant.RedisMessageConstant;
import cn.jiyun.entity.Result;
import cn.jiyun.serivce.OrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Map;

@RestController
@RequestMapping("order")
public class OrderController {
    @Autowired
    private JedisPool jedisPool;

    @Reference
    private OrderService orderService;

    @RequestMapping("submitOrder")
    public Result submitOrder(@RequestBody Map<String,Object> map){
        try {

//            0、校验提交的验证码是否正确
            String validateCode = (String)map.get("validateCode");
            String telephone = (String)map.get("telephone");
            Jedis jedis = jedisPool.getResource();
            String code = jedis.get(RedisMessageConstant.SENDTYPE_ORDER + ":" + telephone);
            if(validateCode.equals(code)){
                return orderService.submitOrder(map);
            }else {
                return new Result(false, MessageConstant.VALIDATECODE_ERROR);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ORDER_FAIL);
        }
    }

    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            Map<String,Object> map = orderService.findById(id);
            return new Result(true, MessageConstant.GET_ORDERSETTING_SUCCESS,map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_ORDERSETTING_FAIL);
        }
    }
}
